package com.ibeeking.found.content.c.rest.service.impl;

import com.ibeeking.found.common.config.prop.FileOssProp;
import com.ibeeking.found.common.utils.UrlUtils;
import com.ibeeking.found.content.c.rest.service.IArticleAuthorService;
import com.ibeeking.found.content.c.rest.service.IArticleService;
import com.ibeeking.found.content.service.common.dos.ArticleAuthorDO;
import com.ibeeking.found.content.service.common.param.ArticleAuthorParam;
import com.ibeeking.found.content.service.common.query.ArticleAuthorPageQuery;
import com.ibeeking.found.content.service.common.query.ArticleAuthorQuery;
import com.ibeeking.found.content.service.common.vo.ArticleAuthorVO;
import com.ibeeking.found.content.service.common.vo.ArticleCount;
import com.ibeeking.found.content.service.mapper.ArticleAuthorMapper;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName ArticleAuthorServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2022-02-18 14:14
 **/
@Service
public class ArticleAuthorServiceImpl extends ServiceImpl<ArticleAuthorMapper, ArticleAuthorDO> implements IArticleAuthorService {

    @Resource
    private ArticleAuthorMapper articleAuthorMapper;
    @Resource
    private FileOssProp fileOssProp;
    @Resource
    private IArticleService articleServiceImpl;

    @Override
    public List<ArticleAuthorVO> list(ArticleAuthorQuery articleAuthorQuery) {
        List<ArticleAuthorDO> articleAuthorDOs = articleAuthorMapper.selectList(new LambdaQueryWrapper<ArticleAuthorDO>()
                .eq(articleAuthorQuery.getPublishStatus() != null, ArticleAuthorDO::getPublishStatus, articleAuthorQuery.getPublishStatus())
                .eq(articleAuthorQuery.getId() != null, ArticleAuthorDO::getId, articleAuthorQuery.getId())
                .in(Collections3.isNotEmpty(articleAuthorQuery.getIds()), ArticleAuthorDO::getId, articleAuthorQuery.getIds())
                .like(StringUtils.isNotBlank(articleAuthorQuery.getNickname()), ArticleAuthorDO::getNickname, articleAuthorQuery.getNickname())
                .orderByDesc(ArticleAuthorDO::getCreateTime)
        );
        List<ArticleAuthorVO> articleAuthorVOS = JsonUtils.jsonToList(articleAuthorDOs, ArticleAuthorVO.class);
        wrapperVO(articleAuthorVOS);
        return articleAuthorVOS;
    }

    @Override
    public Page<ArticleAuthorVO> pageList(ArticleAuthorPageQuery articleAuthorPageQuery) {
        Page<ArticleAuthorDO> pages = articleAuthorMapper.selectPage(new Page<ArticleAuthorDO>(articleAuthorPageQuery.getPageNum(), articleAuthorPageQuery.getPageSize()), new LambdaQueryWrapper<ArticleAuthorDO>());
        Page<ArticleAuthorVO> articleAuthorVOPage = BeanUtil.convertPage(pages, ArticleAuthorVO.class);
        List<ArticleAuthorVO> records = articleAuthorVOPage.getRecords();
        if (Collections3.isNotEmpty(records)) {
            wrapperVO(records);
            //查询文章数
            List<ArticleCount> articleCounts = articleServiceImpl.countByAuthor(records.stream().map(ArticleAuthorVO::getId).collect(Collectors.toList()));
            Map<Long, Integer> articleCountMap = articleCounts.stream().collect(Collectors.toMap(ArticleCount::getId, ArticleCount::getNum));
            records.forEach(x -> x.setArticleNum(articleCountMap.get(x.getId())));
        }

        return articleAuthorVOPage;
    }

    @Override
    public ArticleAuthorVO view(Long id) {
        ArticleAuthorDO articleAuthorDO = articleAuthorMapper.selectById(id);
        ArticleAuthorVO articleAuthorVO = BeanUtil.convertBean(articleAuthorDO, ArticleAuthorVO.class);
        wrapperVO(Collections3.newArrayList(articleAuthorVO));
        return articleAuthorVO;
    }

    public void wrapperVO(List<ArticleAuthorVO> vos) {
        for (ArticleAuthorVO vo : vos) {
            vo.setHeadImageUrl(UrlUtils.addUrlDomainName(fileOssProp.getHost(), vo.getHeadImageUrl()));
        }
    }

    /**
     * 校验唯一性
     *
     * @param param
     */
    public void checkUnique(ArticleAuthorParam param) {
        LambdaQueryWrapper<ArticleAuthorDO> queryWrapper = new LambdaQueryWrapper<ArticleAuthorDO>()
                .ne(param.getId() != null, ArticleAuthorDO::getId, param.getId())
                .eq(StringUtils.isNotBlank(param.getNickname()), ArticleAuthorDO::getNickname, param.getNickname());
        List<ArticleAuthorDO> dos = articleAuthorMapper.selectList(queryWrapper);
        if (Collections3.isNotEmpty(dos)) {
            throw new BusinessException("已经存在相同昵称的作者!");
        }
    }
}
